<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
//路由地址对象
var route = {
    list:"admin/${classNameLower}!get${className}ListWithPage.do?sfunname="+sfunname,
    add:"admin/${classNameLower}!add.do?sfunname="+sfunname,
    update:"admin/${classNameLower}!update.do?sfunname="+sfunname,
    delete:"admin/${classNameLower}!delete.do?sfunname="+sfunname
};
$(function(){
    h=$(window).height();
    w=$(window).width();
    //获取XX列表
    $("#${classNameLower}grid").datagrid({
        title:'xx列表',
        url:route.list,
        singleSelect:true,
        sortName: '',
        sortOrder: '',
        remoteSort: false,
        rownumbers:true,
        idField:'nid',
        collapsible:true,
        striped: true,
        width:w-18,
        height:h-22,
        pageSize:20,
        pageList:[10,20,30,40,50],
        pagination:true,
        toolbar:[{
            text:'添加',
            iconCls:"icon-add",
            disabled:!isadd,
            handler:function(){
                $('#user_window').window({title:"添加XX信息"});
                $("#labelForInitPwd").show();
                $("#user_spwd").show();
                $('#user_window').window('open');
                $('#${classNameLower}Form').form('clear');
                $('#${classNameLower}Form').attr("action",route.add);
            }
        },{
            text:'修改',
            iconCls:"icon-edit",
            disabled:!ismodify,
            handler:function(){
                var row = $('#${classNameLower}grid').datagrid('getSelected');
                if(!row){
                    $.messager.alert('提示','请选择要修改的XX记录!','info');
                    return ;
                }
                $('#user_window').window({title:"修改XX信息"});
                $('#user_window').window('open');
                $('#${classNameLower}Form').form('clear');
                $('#${classNameLower}Form').attr("action",route.update);
                $('#${classNameLower}Form').form('load',{
                	<#list table.columns as column>
                	${column.columnNameLower}:row.${column.columnNameLower}<#if column_has_next>,</#if>
                	</#list>
                });
            }
        },{
            text:'删除',
            iconCls:"icon-remove",
            disabled:!isremove,
            handler:function(){
                var row = $('#${classNameLower}grid').datagrid('getSelected');
                if (!row) {
                    $.messager.alert('提示', '请选择记录!', 'info');
                    return ;
                }
                $.messager.confirm('提示', '确定删除吗?', function(r){
                    if (r){
                        $.ajax({
                            url:route.delete,
                            data:"nid=" + row.nid,
                            type:'post',
                            dataType:'json',
                            success:function(data){
                                if(data.success){
                                    showJsonMsg(data);
                                    refreshUserGrid();
                                }else{
                                    $.messager.alert('提示', data.msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        },'-'],
        columns:[[
                  
<#list table.columns as column>
<#if column.isDateTimeColumn>
{field:'${column.columnNameLower}',title:'${column.columnAlias!}',width:120,align:'center',
    formatter:function(value,rec){
        return new Date(value).toCommonCase();
    }
}<#if column_has_next>,</#if>
<#else>
{field:'${column.columnNameLower}',title:'${column.columnAlias!}',width:120}<#if column_has_next>,</#if>
</#if>
</#list>
        ]],
        onBeforeLoad:function(param){
            $('#user_div .datagrid-toolbar').append($('#user_div #search_div'));
        }
    });


    //“查询”点击事件
    $("#searchBtn").click(function(){
        $("#${classNameLower}grid").datagrid("unselectAll");
        $('#${classNameLower}grid').datagrid("load", {
        	//************
        });
    });

    //编辑窗口关闭按钮的事件
    $("#cancelUserEdit").click(function(){
        $('#user_window').window('close');
        $('#user_window .errorspan').hide();
        $("#userRoleGrid").datagrid('unselectAll');
        $('#${classNameLower}Form').form('clear');
    });

    //编辑窗口确认按钮的事件
    $("#submitUserEdit").click(function(){
        var action = $('#${classNameLower}Form').attr("action");
        if(route.add == action){
                $.ajax({
                    url:route.add,
                    data:$("#${classNameLower}Form").serialize(),
                    type:'post',
                    dataType:'json',
                    success:function(data){
                        userProcessJson(data);
                    }
                });
        }else if(route.update == action){
            $.ajax({
                url:route.update,
                data:$("#${classNameLower}Form").serialize(),
                type:'post',
                dataType:'json',
                success:function(data){
                    userProcessJson(data);
                }
            })
        }
    });
});

//对ajax成功返回结果进行处理
function userProcessJson(data) {
    if(data.success){
        showJsonMsg(data);
        refreshUserGrid();
    }else{
        $.messager.alert('提示', data.msg, 'error');
    }
}

//重新刷新表格
function refreshUserGrid(){
    $('#user_window').window('close');
    if($("#${classNameLower}Form").attr('action') == route.add){
        $('#${classNameLower}grid').datagrid('load',{
        });
    }else{
        $("#${classNameLower}grid").datagrid("reload");
    }
    $('#${classNameLower}Form').form('clear');
}